package org.jivesoftware.openfire.redischat;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jivesoftware.openfire.muc.spi.MUCPersistenceManager;
import org.xmpp.packet.JID;

/**
 * 
 * 房间信息、房间-成员关系
 * 
 * 
 */
public class RedisMUCRoom implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7134390236153225828L;
	// 讨论组名称
	private String name;
	// 讨论组JID
	private JID jid;
	// 是否销毁
	boolean isDestroyed = false;
	// 讨论组描述
	private String description;
	// 是否允许成员修改主题
	private boolean canOccupantsChangeSubject;
	// 最大上限人数
	private int maxUsers;
	// 只允许成员进入
	private boolean membersOnly;
	// 成员是否能够邀请成员加入
	private boolean canOccupantsInvite;
	// 讨论组主题
	private String subject = "";
	// 讨论组ID
	private long roomID = -1;
	// 创建时间
	private Date creationDate;
	// 修改时间
	private Date modificationDate;
	// 是否保存至数据库
	private boolean savedToDB = true;
	// 房间所有者
	private JID owner;
	// 所有成员
	private List<RedisMUCMember> members = new ArrayList<RedisMUCMember>();

	public RedisMUCRoom(String roomname, String mucServiceName) {
		this.name = roomname;
		this.description = roomname;
		Long startTime = System.currentTimeMillis();
		this.creationDate = new Date(startTime);
		this.modificationDate = new Date(startTime);
		this.canOccupantsChangeSubject = MUCPersistenceManager
				.getBooleanProperty(mucServiceName,
						"room.canOccupantsChangeSubject", true);
		this.maxUsers = MUCPersistenceManager.getIntProperty(mucServiceName,
				"room.maxUsers", 50);
		this.membersOnly = MUCPersistenceManager.getBooleanProperty(
				mucServiceName, "room.membersOnly", false);
		this.canOccupantsInvite = MUCPersistenceManager.getBooleanProperty(
				mucServiceName, "room.canOccupantsInvite", true);

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public JID getJid() {
		return jid;
	}

	public void setJid(JID jid) {
		this.jid = jid;
	}

	public boolean isDestroyed() {
		return isDestroyed;
	}

	public void setDestroyed(boolean isDestroyed) {
		this.isDestroyed = isDestroyed;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean isCanOccupantsChangeSubject() {
		return canOccupantsChangeSubject;
	}

	public void setCanOccupantsChangeSubject(boolean canOccupantsChangeSubject) {
		this.canOccupantsChangeSubject = canOccupantsChangeSubject;
	}

	public int getMaxUsers() {
		return maxUsers;
	}

	public void setMaxUsers(int maxUsers) {
		this.maxUsers = maxUsers;
	}

	public boolean isMembersOnly() {
		return membersOnly;
	}

	public void setMembersOnly(boolean membersOnly) {
		this.membersOnly = membersOnly;
	}

	public boolean isCanOccupantsInvite() {
		return canOccupantsInvite;
	}

	public void setCanOccupantsInvite(boolean canOccupantsInvite) {
		this.canOccupantsInvite = canOccupantsInvite;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public long getRoomID() {
		return roomID;
	}

	public void setRoomID(long roomID) {
		this.roomID = roomID;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public Date getModificationDate() {
		return modificationDate;
	}

	public void setModificationDate(Date modificationDate) {
		this.modificationDate = modificationDate;
	}

	public boolean isSavedToDB() {
		return savedToDB;
	}

	public void setSavedToDB(boolean savedToDB) {
		this.savedToDB = savedToDB;
	}

	public JID getOwner() {
		return owner;
	}

	public void setOwner(JID owner) {
		this.owner = owner;
	}

	public void addMemeber(JID memJid, int state) {
		RedisMUCMember m = new RedisMUCMember();
		m.setJid(memJid.asBareJID());
		m.setState(state);
		
		if (!hasMember(memJid)) {
			members.add(m);
		}
	}
	
	public void kickMemeber(JID memJid) {
		for (RedisMUCMember mem:members) {
			if(mem.getJid().getNode().intern() == memJid.getNode().intern()){
				members.remove(mem);
				break;
			}			
		}
	}
	
	public RedisMUCMember getMember(JID memJid) {
		for (RedisMUCMember mem:members) {
			if(mem.getJid().getNode().intern() == memJid.getNode().intern()){
				return mem;
			}			
		}
		return null;
	}

	public RedisMUCMember[] getMembers() {
		return members.toArray(new RedisMUCMember[members.size()]);
	}
	
	//判断某用户是否在此房间
	public boolean hasMember(JID memJid){
		for(RedisMUCMember m:members){
			if( m.getJid().getNode().intern() == memJid.getNode().intern()){
				return true;
			}
		}
		return false;
	}
	
//	public List<RedisMUCMember> getMembers() {
//		return members;
//	}
//
//	public void setMembers(List<RedisMUCMember> members) {
//		this.members = members;
//	}
}
